package spider

import (
	"fmt"
	"github.com/gocolly/colly"
	"gocolly/utils"
	"strings"

	//"gocolly/utils"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"os"
	"sync"
	"time"
)

var wait sync.WaitGroup

func init() {
	wait = sync.WaitGroup{}
}

func SchoolNewAvatar(c *colly.Collector, startUrl string, jar *cookiejar.Jar) {
	c.OnRequest(func(request *colly.Request) {
		fmt.Println("开始.............." + request.URL.Path)

	})
	c.OnResponse(func(response *colly.Response) {

		//sourceDecoding := simplifiedchinese.GBK.NewDecoder()
		//
		//trans := transform.NewReader(bytes.NewReader(response.Body), sourceDecoding)
		//utf8Body, err := io.ReadAll(trans)
		//if err != nil {
		//	panic("转换失败：" + err.Error())
		//}
		//response.Body = utf8Body
	})
	c.OnError(func(response *colly.Response, err error) {
		if err != nil {
			panic("请求失败：" + err.Error())
		}
	})
	c.OnHTML("#wrapper > div.cont_l.in > div.scores_List", func(element *colly.HTMLElement) {
		element.ForEach("dl img", func(i int, edl *colly.HTMLElement) {
			go func() {
				wait.Add(1)
				collegeName := edl.Attr("alt")

				imgUrl := edl.Attr("src")
				imgUrl = edl.Request.AbsoluteURL(imgUrl)

				parseUrl, err := url.Parse(imgUrl)
				if err != nil {
					panic("图片地址错误：" + err.Error())
				}

				strs := strings.Split(parseUrl.Path, "/")

				fileName := strs[len(strs)-1]
				fileNameS := strings.Split(fileName, ".")
				imgType := fileNameS[len(fileNameS)-1]
				imgName := collegeName + "." + imgType

				downImg := utils.NewDown(
					imgUrl,
					parseUrl.Host,
					"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
					jar,
					"./college_avatars/",
					imgName)
				imgPath := downImg.DownImage()
				fmt.Println(imgPath)
				defer wait.Done()
			}()
		})

	})

	c.OnHTML("#wrapper > div.cont_l.in > div:nth-child(4) > ul > li:nth-child(3) > a", func(element *colly.HTMLElement) {
		wait.Wait()
		url := element.Attr("href")
		if url == "" {
			fmt.Println("爬取完成.......")
			os.Exit(0)
		}
		fmt.Println("严迟....")
		time.Sleep(time.Second * 20)

		c.Visit(element.Attr("href"))
	})

	c.Visit(startUrl)
}

func Example() {
	startUrl := "https://college.gaokao.com/schlist/p106/"
	startUrlParse, err := url.Parse(startUrl)
	if err != nil {
		panic("解析url错误：" + err.Error())
	}

	c := colly.NewCollector(
		colly.AllowedDomains(startUrlParse.Host),
		colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"),
	)
	c.WithTransport(&http.Transport{
		TLSClientConfig: nil,
	})
	initCookie := []*http.Cookie{
		{
			Name:   "HMACCOUNT",
			Value:  "F6E46E6EEDEF0AC0",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "Hm_lpvt_aa27487f630124a75eaf9c8ac900811c",
			Value:  "1731129436",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "Hm_lpvt_c35069925979266b098765567b245009",
			Value:  "1731129436",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "Hm_lvt_aa27487f630124a75eaf9c8ac900811c",
			Value:  "1731121432",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "Hm_lvt_c35069925979266b098765567b245009",
			Value:  "1731121432",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "sajssdk_2015_cross_new_user",
			Value:  "1",
			Domain: ".gaokao.com",
			Path:   "/",
		},
		{
			Name:   "sensorsdata2015jssdkcross",
			Value:  "%7B%22distinct_id%22%3A%221930ee0cfcc30d-0f760fdf696b878-26001e51-2073600-1930ee0cfcd870%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%221930ee0cfcc30d-0f760fdf696b878-26001e51-2073600-1930ee0cfcd870%22%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzMGVlMGVlZDExMzFjLTBjZmNiNzcyZjhjNDE3OC0yNjAwMWU1MS0yMDczNjAwLTE5MzBlZTBlZWQyMTMyNyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D",
			Domain: ".gaokao.com",
			Path:   "/",
		},
	}

	jar, _ := cookiejar.New(nil)
	jar.SetCookies(startUrlParse, initCookie)
	c.SetCookieJar(jar)

	SchoolNewAvatar(c, startUrl, jar)
}
